﻿using QS.Tool.Common.WaterMark;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QS.Tool.MainEntry
{
    public partial class FrmWaterMark : Form
    {
        public FrmWaterMark()
        {
            InitializeComponent();
            BindSite();
            cmbImgType.SelectedIndex = 0;
            cmbSite.SelectedIndex = 0;
        }

        private void rdbImg_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbImg.Checked)
            {
                btnSelFile.Enabled = true;
            }
            else
            {
                btnSelFile.Enabled = false;
            }
        }

        private void rdbFont_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbFont.Checked)
            {
                btnSelFile.Enabled = false;
            }
            else
            {
                btnSelFile.Enabled = true;
            }
        }
        /// <summary>
        /// 选择水印图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelFile_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 选择需处理的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelSource_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Title = "请选择文件";
            ofd.Filter= "图像文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";
            if (ofd.ShowDialog()== DialogResult.OK)
            {
                var file = ofd.FileName;
                labSourcePath.Text = file;
            }
        }
        /// <summary>
        /// 选择保存图片路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelSavePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择保存文件路径";
            if (fbd.ShowDialog()== DialogResult.OK)
            {
                labSavePath.Text = fbd.SelectedPath;
            }
        }
        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            var imgPath = labSourcePath.Text;
            var filename = labSavePath.Text + "\\"+txtSaveName.Text+cmbImgType.Text;
            var watermarkText = txtFile.Text;
            var watermarkStatus = EnumToValue(cmbSite.Text);
            var quality = 50;
            var fontsize = 100;
            var num = nudWaterNum.Value;
            AddImageSignText(imgPath, filename, watermarkText, watermarkStatus, quality, fontsize, 10);
        }
        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="filename">保存文件名及路径</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="fontsize">字体大小</param>
        /// <param name="fontname">字体</param>
        private void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, int fontsize, int opacity = 100, string fontname = "微软雅黑")
        {
            WaterMarkHelper.AddImageSignText(imgPath, filename, watermarkText, watermarkStatus, quality, fontsize,opacity, fontname);
        }
        private void BindSite()
        {
            cmbSite.Items.Clear();
            foreach (WaterMarkEnum item in Enum.GetValues(typeof(WaterMarkEnum)))
            {
                cmbSite.Items.Add(item);
            }
        }

        private int EnumToValue(string enumName)
        {
            int val = 0;
            foreach (WaterMarkEnum item in Enum.GetValues(typeof(WaterMarkEnum)))
            {
                if (item.ToString()== enumName)
                {
                    val = (int)item;
                }
            }
            return val;
        }

        private void cmbSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cmbSite.SelectedItem.ToString()== WaterMarkEnum.随机.ToString())
            //{
            //    nudWaterNum.Enabled = true;
            //}
            //else
            //{
            //    nudWaterNum.Value = 1;
            //    nudWaterNum.Enabled = false;
            //}
        }
    }
}
